package com.mbatis.leeCode;

import org.junit.Test;

import java.util.HashMap;

/**
 * @Classname twoSum
 * @Date 2021/5/20 17:19
 * @Created by zhang.yuming
 */
public class twoSum {

    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (nums[i] + nums[j] == target) {
                    System.out.println("i="+i+"j="+j);
//                    return new int[]{i, j};
                }
            }
        }
        return new int[0];
    }


    @Test
    public void ts(){
        int[] nums = new int[]{1,3,5,7};
        int target = 6;
        int[] ints = twoSum(nums, target);
        for (int i = 0; i < ints.length; i++) {
            System.out.println(ints[i]);
        }

    }

    @Test
    public void twoSum2(){
        int[] nums = new int[]{1,5,7,11};
        int target = 8;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target-nums[i])){ // 1
                System.out.println("i"+map.get(nums[i])+"i="+i);
            }
            map.put(nums[i],i);  // 2
        }
        // 第一次执行  8-1=7   map中不存在      放入map  1,0
        // 第二次执行  8-5=3   map中不存在      放入map  5,1
        // 第三次执行


    }

}

